# -*- coding: utf-8 -*-
from datetime import timedelta
from time import time
from utils.operators.rich_sql_sensor import RichSqlSensor
# from jms.dm.dm_whole_decide_duty_flow_city_dt import jms_dm__dm_whole_decide_duty_flow_city_dt

label = f"dm_whole_decide_duty_flow_city_dt_{int(time())}"
timeout = timedelta(minutes=30).seconds
doris_jms_dm__dm_whole_decide_duty_flow_city_dt = RichSqlSensor(
    task_id='doris_jms_dm__dm_whole_decide_duty_flow_city_dt',
    pool='broker_load_pool',
    email=['suning@jtexpress.com','yl_bigdata@yl-scm.com'],
    conn_id='new_doris',
    pre_sql=f"""
    TRUNCATE TABLE jms_dm.dm_whole_decide_duty_flow_city_dt PARTITION (
         p{{{{ execution_date | cst_ds_nodash }}}}
    );
    LOAD LABEL jms_dm.{label} (
        DATA INFILE(
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_whole_decide_duty_flow_city_dt/dt={{{{ execution_date | cst_ds }}}}/*")
        INTO TABLE dm_whole_decide_duty_flow_city_dt
        FORMAT AS 'PARQUET'(
             start_city_code
            ,start_city_name
            ,start_provider_code
            ,start_provider_name
            ,start_agent_code
            ,start_agent_name
            ,end_city_code
            ,end_city_name
            ,end_provider_code
            ,end_provider_name
            ,end_agent_code
            ,end_agent_name
            ,platform_effect
            ,ziyou_platform_effect
            ,cainiao_platform_effect
            ,order_source_code
            ,order_source_name
            ,duty_type
            ,jingtai_lyqs_cnt
            ,dizhi_jxcw_cnt
            ,jingtai_my_cnt
            ,jiedanhou_ls_cnt
            ,jiaojian_jbcw_cnt
            ,cuojiao_cnt
            ,jiaojian_bzd_cnt
            ,zhongzhuan_jbcw_cnt
            ,cuofa_cnt
            ,fengche_wd_cnt
            ,caozuo_yw_cnt
            ,fache_wd_cnt
            ,yunshu_wd_cnt
            ,jingang_zybjs_cnt
            ,zhongzhuan_cf_cnt
            ,sanhuo_bjs_cnt
            ,paijian_cf_cnt
            ,qianshou_wd_cnt
            ,duty_cnt
            ,aging_sign_date
        )
        columns from path as (dt)
        set(
             start_city_code          =  start_city_code
            ,start_city_name          =  start_city_name
            ,start_provider_code      =  start_provider_code
            ,start_provider_name      =  start_provider_name
            ,start_agent_code         =  start_agent_code
            ,start_agent_name         =  start_agent_name
            ,end_city_code            =  end_city_code
            ,end_city_name            =  end_city_name
            ,end_provider_code        =  end_provider_code
            ,end_provider_name        =  end_provider_name
            ,end_agent_code           =  end_agent_code
            ,end_agent_name           =  end_agent_name
            ,platform_effect          =  platform_effect
            ,ziyou_platform_effect    =  ziyou_platform_effect
            ,cainiao_platform_effect  =  cainiao_platform_effect
            ,order_source_code        =  order_source_code
            ,order_source_name        =  order_source_name
            ,duty_type                =  duty_type
            ,jingtai_lyqs_cnt         =  jingtai_lyqs_cnt
            ,dizhi_jxcw_cnt           =  dizhi_jxcw_cnt
            ,jingtai_my_cnt           =  jingtai_my_cnt
            ,jiedanhou_ls_cnt         =  jiedanhou_ls_cnt
            ,jiaojian_jbcw_cnt        =  jiaojian_jbcw_cnt
            ,cuojiao_cnt              =  cuojiao_cnt
            ,jiaojian_bzd_cnt         =  jiaojian_bzd_cnt
            ,zhongzhuan_jbcw_cnt      =  zhongzhuan_jbcw_cnt
            ,cuofa_cnt                =  cuofa_cnt
            ,fengche_wd_cnt           =  fengche_wd_cnt
            ,caozuo_yw_cnt            =  caozuo_yw_cnt
            ,fache_wd_cnt             =  fache_wd_cnt
            ,yunshu_wd_cnt            =  yunshu_wd_cnt
            ,jingang_zybjs_cnt        =  jingang_zybjs_cnt
            ,zhongzhuan_cf_cnt        =  zhongzhuan_cf_cnt
            ,sanhuo_bjs_cnt           =  sanhuo_bjs_cnt
            ,paijian_cf_cnt           =  paijian_cf_cnt
            ,qianshou_wd_cnt          =  qianshou_wd_cnt
            ,duty_cnt                 =  duty_cnt
            ,aging_sign_date          =  aging_sign_date
            ,dt                       =  dt
        )
    )
    WITH BROKER '{{{{ var.json.doris_brokers | random_choice }}}}'
    PROPERTIES ('timeout'='{timeout}', 'max_filter_ratio'='0.0')""",
    poke_sql=f"SHOW LOAD FROM jms_dm WHERE label = '{label}' ORDER BY CreateTime DESC LIMIT 1",
    sql_on_kill=f"CANCEL LOAD FROM jms_dm WHERE LABEL = '{label}'",
    success=lambda r: r[2] == 'FINISHED',
    failure=lambda r: (r[2] is not None and r[2] == 'CANCELLED', str(r[7])),
    poke_interval=60,
    execution_timeout=timedelta(seconds=timeout + 120), )

# doris_jms_dm__dm_whole_decide_duty_flow_city_dt << jms_dm__dm_whole_decide_duty_flow_city_dt
